//
//  DateBasicReminderViewController.m
//  Voice2AlarmNote
//
//  Created by onemade on 14-11-3.
//  Copyright (c) 2014年 CNPC. All rights reserved.
//

#import "DateBasicReminderViewController.h"

@interface DateBasicReminderViewController ()

@end

@implementation DateBasicReminderViewController

#pragma mark - life cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    self.remiderTitleTextField.delegate = self;
}

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark - button actions
- (IBAction)setReminder:(id)sender {
    if ([[self.remiderTitleTextField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length] < 1) {
        [self.remiderTitleTextField becomeFirstResponder];
        return;
    }
    if (self.eventStore == nil) {
        self.eventStore = [[EKEventStore alloc] init];
        [self.eventStore requestAccessToEntityType:EKEntityTypeReminder completion:^(BOOL granted, NSError *error){
            if (!granted) {
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提醒" message:@"没有授权" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                [alert show];
            }
        }];
    }else{
        [self createReminder];
    }
}

- (IBAction)setLocationReminder:(id)sender {
    if ([[self.remiderTitleTextField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length] < 1) {
        [self.remiderTitleTextField becomeFirstResponder];
        return;
    }
    if (self.eventStore == nil) {
        self.eventStore = [[EKEventStore alloc] init];
        [self.eventStore requestAccessToEntityType:EKEntityTypeReminder completion:^(BOOL granted, NSError *error){
            if (!granted) {
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提醒" message:@"没有授权" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                [alert show];
            }
        }];
    }else{
        self.locationManager = [[CLLocationManager alloc] init];
        self.locationManager.delegate = self;
        self.locationManager.distanceFilter = kCLDistanceFilterNone;
        self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
        
        [self.locationManager startUpdatingLocation];
    }
}

#pragma mark - UITextField delegate
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [self.remiderTitleTextField resignFirstResponder];
    return YES;
}

#pragma mark - CLLocationManagerDelegate Methods
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    [self.locationManager stopUpdatingLocation];
    EKReminder *reminder = [EKReminder reminderWithEventStore:self.eventStore];
    reminder.title = self.remiderTitleTextField.text;
    reminder.calendar = [self.eventStore defaultCalendarForNewReminders];
    EKStructuredLocation *location = [EKStructuredLocation locationWithTitle:@"Current Location"];
    location.geoLocation = [locations lastObject];
    location.radius= 1*1000;
    EKAlarm *alarm = [[EKAlarm alloc] init];
    alarm.structuredLocation = location;
    alarm.proximity = EKAlarmProximityLeave;
    [reminder addAlarm:alarm];
    
    NSError *error = nil;
    [self.eventStore saveReminder:reminder commit:YES error:&error];
    if (error) {
        NSLog(@"Fail to set reminder: %@", error);
    }
}

#pragma mark - customize methods
-(void)createReminder{
    EKReminder *reminder = [EKReminder reminderWithEventStore:self.eventStore];
    reminder.title = self.remiderTitleTextField.text;
    reminder.calendar = [self.eventStore defaultCalendarForNewReminders];
    NSDate *date = [self.datePicker date];
    EKAlarm *alarm = [EKAlarm alarmWithAbsoluteDate:date];
    [reminder addAlarm:alarm];
    NSError *error = nil;
    [self.eventStore saveReminder:reminder commit:YES error:&error];
    if (error) {
        NSLog(@"error = %@",error);
    }
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [[event allTouches] anyObject];
    if ([self.remiderTitleTextField isFirstResponder] && [touch view] != self.remiderTitleTextField) {
        [self.remiderTitleTextField resignFirstResponder];
    }
    [super touchesBegan:touches withEvent:event];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/


- (IBAction)goBack:(id)sender {
    [self dismissViewControllerAnimated:YES completion:^{}];
}
@end
